//
//  LLViewController.m
//  lrlzapp
//
//  Created by dl on 16/2/22.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLViewController.h"
#import "LLViewModel.h"

@interface LLViewController ()
@property(nonatomic, strong, readwrite) LLViewModel *viewModel;
@end

@implementation LLViewController

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    LLViewController *viewController = [super allocWithZone:zone];

    @weakify(viewController)
    [[viewController
            rac_signalForSelector:@selector(viewDidLoad)]
            subscribeNext:^(id x) {
                @strongify(viewController)
                [viewController bindViewModel];
            }];

    return viewController;
}

- (LLViewController *)initWithViewModel:(LLViewModel *)viewModel {
    self = [super init];
    if (self) {
        self.viewModel = viewModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

//    self.automaticallyAdjustsScrollViewInsets = YES;
//    self.extendedLayoutIncludesOpaqueBars = YES;
    [self.navigationController setNavigationBarHidden:NO];

    [[self.navigationItem.rightBarButtonItems.rac_sequence.signal
            concat:self.navigationItem.leftBarButtonItems.rac_sequence.signal]
            subscribeNext:^(UIBarButtonItem *item) {
                item.tintColor = [UIColor colorWithPatternImage:item.image];
            }];
}

- (void)bindViewModel {
    // System title view
    RAC(self, title) = RACObserve(self.viewModel, title);
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.viewModel.willDisappearSignal sendNext:nil];
}

#pragma mark
#pragma mark custom

- (UIButton *)addRightNavItemWithImage:(UIImage *)image {
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SUIT_W(47), SUIT_H(30))];
    [btn setImage:image forState:UIControlStateNormal];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    btn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    return btn;
}

@end
